package com.ruoyi.web.wuchuan.command;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.ruoyi.web.wuchuan.dto.*;
import com.ruoyi.web.wuchuan.enmu.Param;
import com.ruoyi.web.wuchuan.handle.PDFModule;
import com.ruoyi.web.wuchuan.util.PDFutil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class Issues implements PDFModule {
    private static final String pdfName = "生态适宜报告-作业区域报告" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".pdf";
    private static final String tempPath = System.getProperty("java.io.tmpdir");
    private final String zipName = "生态适宜报告-作业区域报告" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".zip";

    @Override
    public String getZipName() {
        return zipName;
    }

    @Override
    public String analyticalDataByType(PDFProperties pdfProperties, String uuid) {
        Document document = new Document(PageSize.A4);
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(tempPath+pdfName));
            document.addTitle("example of PDF");
            document.open();
            document.add(CretePDF(pdfProperties,uuid));
        } catch (FileNotFoundException | DocumentException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
        return tempPath+pdfName;
    }
    private PdfPTable CretePDF(PDFProperties pdfProperties,String uuid) {
        PdfPTable table = new PdfPTable(Param.tittleSzls);
        table.setSpacingBefore(20f);//设置页边距
        table.setWidthPercentage(100);//设置表格宽度为100%
        PDFutil pdFutil = new PDFutil();
        //一级标题
        table.addCell(pdFutil.getCell("作业区域生态适宜报告", pdFutil.getFont(Param.tittleFontSize, true), Param.tittleSzls, Param.tittleHigh, 1));
        table.addCell(pdFutil.getCell("作业区域基础信息", pdFutil.getFont(Param.tittleFontSize, true), Param.tittleSzls, Param.tittleHigh, 0));
        BaseData baseData = pdfProperties.getBaseData();
        //生成key
        table.addCell(pdFutil.getCell("面积(平方米)", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls, Param.cloHigh, 0));
        table.addCell(pdFutil.getCell(baseData.getMj(), pdFutil.getFont(Param.clofontSize, false), Param.cloSzls, Param.cloHigh, 0));
        table.addCell(pdFutil.getCell("生态切片区域数量", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls, Param.cloHigh, 0));
        table.addCell(pdFutil.getCell(baseData.getSl(), pdFutil.getFont(Param.clofontSize, false), Param.cloSzls, Param.cloHigh, 0));
        table.addCell(pdFutil.getCell("中心点经度", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls, Param.cloHigh, 0));
        table.addCell(pdFutil.getCell(baseData.getJd(), pdFutil.getFont(Param.clofontSize, false), Param.cloSzls, Param.cloHigh, 0));
        table.addCell(pdFutil.getCell("中心点维度", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls, Param.cloHigh, 0));
        table.addCell(pdFutil.getCell(baseData.getWd(), pdFutil.getFont(Param.clofontSize, false), Param.cloSzls, Param.cloHigh, 0));
        table.addCell(pdFutil.getCell("中心点所属行政区域", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls, Param.cloHigh, 0));
        table.addCell(pdFutil.getCell(baseData.getXzqmc(), pdFutil.getFont(10, false), Param.cloSzls * 3, Param.cloHigh, 0));
        table.addCell(pdFutil.getCell("生态功能区统计分析", pdFutil.getFont(Param.tittleFontSize, true), Param.tittleSzls, Param.tittleHigh, 0));
        table.addCell(pdFutil.getCell("区域适宜类型", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls, Param.cloHigh, 0));
        table.addCell(pdFutil.getCell("切片区域数量", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls , Param.cloHigh, 0));
        table.addCell(pdFutil.getCell("面积(平方米)", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls, Param.cloHigh, 0));
        table.addCell(pdFutil.getCell("平均适宜分值", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls, Param.cloHigh, 0));
        List<TjfxDTO> tjfxDTOS = pdfProperties.getTjfx();
        tjfxDTOS.forEach(tjfxDTO -> {
            table.addCell(pdFutil.getCell(tjfxDTO.getSylx(), pdFutil.getFont(Param.clofontSize, false), Param.cloSzls, Param.cloHigh, 0));
            table.addCell(pdFutil.getCell(tjfxDTO.getSl(), pdFutil.getFont(Param.clofontSize, false), Param.cloSzls, Param.cloHigh, 0));
            table.addCell(pdFutil.getCell(tjfxDTO.getMj(), pdFutil.getFont(Param.clofontSize, false), Param.cloSzls, Param.cloHigh, 0));
            table.addCell(pdFutil.getCell(tjfxDTO.getSyfz(), pdFutil.getFont(Param.clofontSize, false), Param.cloSzls, Param.cloHigh, 0));
        });
        List<GnqmxDTO> gnqmxs = pdfProperties.getGnqmx();
        gnqmxs.forEach(gnqmxDTO -> {
            table.addCell(pdFutil.getCell("生态功能区明细", pdFutil.getFont(Param.tittleFontSize, true), Param.tittleSzls, Param.tittleHigh, 0));
            BaseData baseData1 = gnqmxDTO.getBaseData();
            table.addCell(pdFutil.getCell("功能区切片编号", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls, Param.cloHigh, 0));
            table.addCell(pdFutil.getCell(baseData1.getBh(), pdFutil.getFont(Param.clofontSize, false), Param.cloSzls, Param.cloHigh, 0));
            table.addCell(pdFutil.getCell("面积(平方米)", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls, Param.cloHigh, 0));
            table.addCell(pdFutil.getCell(baseData1.getMj(), pdFutil.getFont(Param.clofontSize, false), Param.cloSzls, Param.cloHigh, 0));
            table.addCell(pdFutil.getCell("适宜类型", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls, Param.cloHigh, 0));
            table.addCell(pdFutil.getCell(baseData1.getSylx(), pdFutil.getFont(Param.clofontSize, false), Param.cloSzls, Param.cloHigh, 0));
            table.addCell(pdFutil.getCell("适宜总分值", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls, Param.cloHigh, 0));
            table.addCell(pdFutil.getCell(baseData1.getSyfz(), pdFutil.getFont(Param.clofontSize, false), Param.cloSzls, Param.cloHigh, 0));
            List<FlfzDTO> flfzs = gnqmxDTO.getFlfz();
            table.addCell(pdFutil.getCell("生态分类", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls, Param.cloHigh, 0));
            table.addCell(pdFutil.getCell("匹配分值", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls, Param.cloHigh, 0));
            table.addCell(pdFutil.getCell("加权分值", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls, Param.cloHigh, 0));
            table.addCell(pdFutil.getCell("适宜得分", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls, Param.cloHigh, 0));
            flfzs.forEach(flfzDTO -> {
                table.addCell(pdFutil.getCell(flfzDTO.getStfl(), pdFutil.getFont(Param.clofontSize, false), Param.cloSzls, Param.cloHigh, 0));
                table.addCell(pdFutil.getCell(flfzDTO.getPpfz(), pdFutil.getFont(Param.clofontSize, false), Param.cloSzls, Param.cloHigh, 0));
                table.addCell(pdFutil.getCell(flfzDTO.getJqfz(), pdFutil.getFont(Param.clofontSize, false), Param.cloSzls, Param.cloHigh, 0));
                table.addCell(pdFutil.getCell(flfzDTO.getSydf(), pdFutil.getFont(Param.clofontSize, false), Param.cloSzls, Param.cloHigh, 0));

            });
        });
        return table;
    }

}
